var api = new Api();
var isphpw = 0;
var isKpw = 1;
//输入框获得焦点/失去焦点时，改变icon颜色
$('input').focus(function(){
	$(this).parents('li').removeClass('alert');
	$(this).parents('li').addClass('focus');
});
$('input').blur(function(){
	$(this).parents('li').removeClass('focus');
});
//选择是否记住密码
$('#forget').click(function(){
	$(this).toggleClass('cur');
	if ($(this).hasClass('cur')) {
		isKpw = 1;
	} else {
		isKpw = 0;
	}
});
//切换动态密码登录和普通密码登录状态
$('#dynamic').click(function(){
	$('#username').parents('li').removeClass('alert');
	$('#password').parents('li').removeClass('alert');
	$('#dynamicPassword').parents('li').removeClass('alert');
	$('#errorTip').addClass('hide');
	$('.js-normal').addClass('hide');
	$('.js-dynamic').removeClass('hide');
	$('.js-forget').addClass('hide');
	isphpw = 1;
});
$('#normal').click(function(){
	$('#username').parents('li').removeClass('alert');
	$('#password').parents('li').removeClass('alert');
	$('#dynamicPassword').parents('li').removeClass('alert');
	$('#errorTip').addClass('hide');
	$('.js-dynamic').addClass('hide');
	$('.js-normal').removeClass('hide');
	$('.js-forget').removeClass('hide');
	isphpw = 0;
});
$("#username").bind({"focus":function () {
}, "blur":function () {
	var username = $('#username').val();
	if (validateRules.isNull(username)) {
		showMsg("username","账号不能为空");
	} else {
		if(isphpw == 1) {
			if(!validateRules.isMobile(username)) {
				validateUserName(username);
	    	}
		}
	}
}});
$("#password").bind({"focus":function () {
}, "blur":function () {
	var password = $('#password').val();
	if (validateRules.isNull(password)) {
		showMsg("password","密码不能为空");
	}
}});
$("#dynamicPassword").bind({"focus":function () {
}, "blur":function () {
	var dynamicPassword = $('#dynamicPassword').val();
	if (validateRules.isNull(dynamicPassword)) {
		showMsg("dynamicPassword","动态密码不能为空");
	}
}});

$('#getDypw').click(function(){
	var flag = false;
	var username = $('#username').val();
	if(!validateRules.isMobile(username)) {
		if(!validateUserName(username)) {
			return;
		}
	}
	var params = {
		"userName" : username
	};
	api.localAjaxForJson(api.localSendDyPassword, params, function(json) {
		if (json.code != 1) {
			$('#errorTip').removeClass('hide');
			$('#errorTip').html('<p>'+json.tip.replace("请求处理失败：","")+'</p>');
		} else {
			flag = true;
		}
	}, "post");
	if (flag) {
		$("#getDypw").text(120);
		$('#getDypw').addClass("btn-disabled");
		$('#getDypw').attr("disabled","disabled");
		window.setInterval("run();", 1000);
	}
});

$('#login').click(function(){
	var username = $('#username').val();
	var password = $('#password').val();
	var dynamicPassword = $('#dynamicPassword').val();
	var platform = $('platform').val();
	if (validate(username, dynamicPassword, password)) {
		var params = {
			"userName" : username,
			"password" : isphpw == 1 ? dynamicPassword : password,
			"isphpw" : isphpw,
			"isKpw" : isKpw,
			"ReturnUrl" : $('#returnUrl').val(),
			"platform" : platform
		};
		api.localAjaxForJson(api.localUserLogin, params, function getJSON(json) {
			if (json.code != 1) {
				$('#errorTip').removeClass('hide');
				$('#errorTip').html('<p>'+json.tip.replace("请求处理失败：","")+'</p>');
			} else {
				window.location = json.ReturnUrl;
			}
		}, "post");
	}
});

function validateUserName(username) {
	var flag = false;
	var params = {
		"username" : username
	};
	api.localAjaxForJson(api.localIsbindPhone, params, function(json) {
		if (json.code != 1) {
			showMsg("username",json.tip);
		} else {
			flag = true;
		}
	}, "post");
	return flag;
}

function run(){
	var s = $("#getDypw");
	var sj=s.text();
	if(sj == 0){
		s.removeClass("btn-disabled");
		s.removeAttr("disabled","disabled");
		return false;
	}
	s.text(sj * 1 - 1);
}

function showMsg(id,tip) {
	$('#'+id).parents('li').addClass('alert');
	$('#errorTip').removeClass('hide');
	$('#errorTip').html('<p>'+tip+'</p>');
}

function validate(username, dynamicPassword, password) {
	if (validateRules.isNull(username)) {
		showMsg("username", "账号不能为空");
		return false;
	}
	if (isphpw == 1) {
		if(!validateRules.isMobile(username)) {
			if(!validateUserName(username)) {
				return false;
			}
		}
		if(validateRules.isNull(dynamicPassword)) {
			showMsg("dynamicPassword", "动态密码不能为空");
			return false;
		}
	}
	if (isphpw == 0 && validateRules.isNull(password)) {
		showMsg("password", "密码不能为空");
		return false;
	}
	return true;
}